#include "WeatherDevice.h"
#include "Main/GateWay.h"
WeatherDevice::WeatherDevice(QObject *parent) :
    BaseInterface<WeatherDevice>(parent)
{

    m_pInstance = this;
       m_temperature="00";
       m_humidity="00";
        httpProtocol = new HttpProtocol();
         this->registerProtocol(httpProtocol);

    getWeatherTimer = new QTimer(this);
    connect(getWeatherTimer, SIGNAL(timeout()), this, SLOT(getWeatherSlot()));
    getWeatherTimer->setInterval(10*1000);
    getWeatherTimer ->start();

    registerCmdHandler(getGaoDeWeatherCmd::FunctionCode,getGaoDeWeatherCmd::staticMetaObject, &WeatherDevice::getGaoDeWeatherCmdHandle);

}

void WeatherDevice::getGaoDeWeatherCmdHandle(PortStruct source, QSharedPointer<BaseCmd> sharedCmd)
{
    Q_UNUSED(source);
    qDebug()<<"!~~~~~WeatherDevice::getGaoDeWeatherCmdHandle";
    getGaoDeWeatherCmd  *_getGaoDeWeatherCmd = qobject_cast<getGaoDeWeatherCmd*>(sharedCmd.data());
   qDebug()<<_getGaoDeWeatherCmd->getTemperature()<<","<<_getGaoDeWeatherCmd->getHumidity();

   SceneListXml *sceneListXml = SceneListXml::getInstance();
   ControlPointStruct controlPoint;


   CurrentPointStore *currentPointStore = CurrentPointStore::getInstance();
   QList<ControlPointStruct> logicPointList;


   controlPoint.setAddress(398);
   controlPoint.setValue(_getGaoDeWeatherCmd->getTemperature().toInt());
   controlPoint.setDelayTime(0);
   if(m_temperature != _getGaoDeWeatherCmd->getTemperature())
   {
   sceneListXml->joinDelayTimeList(controlPoint);
   m_temperature = _getGaoDeWeatherCmd->getTemperature();
   }
    logicPointList.append(controlPoint);


   controlPoint.setAddress(397);
   controlPoint.setValue(_getGaoDeWeatherCmd->getHumidity().toInt());
   controlPoint.setDelayTime(10);
   if(m_humidity != _getGaoDeWeatherCmd->getHumidity())
   {
   sceneListXml->joinDelayTimeList(controlPoint);
   m_humidity = _getGaoDeWeatherCmd->getHumidity();
   }

   logicPointList.append(controlPoint);

   currentPointStore->insert(logicPointList);


   // m_token = bagetTokenCmd->getToken();
}

void WeatherDevice::getWeatherSlot()
{
        qDebug()<<"~~~WeatherDevice::getWeatherSlot";
        getGaoDeWeatherCmd *_getGaoDeWeatherCmd = new getGaoDeWeatherCmd();
          //  bagetPointInfoCmd->setToken(DataTypeConver::EnCodeURL(m_token));
          //  bagetPointInfoCmd->setDevID(devID);
            QSharedPointer<BaseCmd> sharedBAGetPointInfoCmd(_getGaoDeWeatherCmd);


            PortStruct target;

            foreach (const BasePort *port , m_portList)
            {
                 if((port->portParam().type() == "HttpClient" && port->portParam().name() == "HttpClient"))
                 {
                     qDebug()<<"~~~WeatherDevice::getWeatherSlot  port HttpClient";
                     target = port->portParam();
                 }
            }

            this->sendCmd(target,sharedBAGetPointInfoCmd);
}
